/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package image.diffussion;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;

/**
 *
 * @author tommy
 */
public class IsotropicDiffussion extends DiffusionImpl {
	private int t; // num of iterations (t := time)
	private double kappa; // free parameter kappa of the g(|gradient(image(x,y,t)|)
	
	public IsotropicDiffussion(BufferedImage src, double kappa, int t) {
		super(src);
		this.t = t;
		this.kappa = kappa;
	}
	
	@Override
	public BufferedImage diffuse() {
		return super.diffuse(t);
	}
	
	@Override
	protected double getDiffusedPixel(int t, int x, int y, int b, Raster inRaster) {
		int I_t; // intensity value at time t @coords (x,y)
		double[] gradient_It = new double[4]; // derivatives of I(x,y) at N, S, E, W in that order
		double val = 0;
		
		I_t = inRaster.getSample(x, y, b);
		gradient_It[0] = inRaster.getSample(x, y-1, b) - I_t; // N
		gradient_It[1] = inRaster.getSample(x, y+1, b) - I_t; // S
		gradient_It[2] = inRaster.getSample(x+1, y, b) - I_t; // E
		gradient_It[3] = inRaster.getSample(x-1, y, b) - I_t; // W
		
		for (int i = 0 ; i < gradient_It.length ; i++) {
			// we do not use sqrt because it's (sqrt(gradient^2))^2 = 
			// gradient^2
			val += gradient_It[i];
		}
		
		val *= 0.25;
		val += I_t;
		
		// finally val: I_t + 1/4 * sum_i=1:4(c_t(x,y) * grad(I_t(x,y)))
		return val;
	}
	
}
